<?
include '../webinit.php';
include '../database/dbhelper.php';
include '../include/paging.php';


if (!empty($_GET['del'])) {
    if ($_GET['del'] == "true") {
        $id = $_GET['id'];
    }
    $hoadon = new hoadon();
    $hoadon = hoadondb::get_by_id($id);
    $hoadon->isdeleted = 'Y';
    hoadondb::update($hoadon);
}

$search_keys = "";
$querykey = "&search_keys=";
if (!empty($_GET["search_keys"])) {
    $search_keys = $_GET["search_keys"];
    $querykey = $querykey.$search_keys;
}
if (!empty($_GET["pay"])) {    
    $querykey = $querykey."&pay=".$_GET["pay"];
} else {
    //mac dinh, hien thi tat ca pay = 1
    $querykey = $querykey."&pay=1";
}

$list_hoadon = null;

if ($search_keys != "") {
    $list_hoadon = hoadondb::search($search_keys, ($page - 1) * $offset, $offset, TRUE, FALSE);
    $totals = hoadondb::search($search_keys, ($page - 1) * $offset, $offset, FALSE, TRUE);
} else {
    $list_hoadon = hoadondb::search($search_keys, ($page - 1) * $offset, $offset, TRUE, FALSE);
    $totals = hoadondb::search($search_keys, ($page - 1) * $offset, $offset, FALSE, TRUE);
    //echo $totals;
}

function hien_thi_thanh_toan($value = "") {
    if ($value == "TraDu") {
        return "Trả đủ";
    }
    if ($value == "TraGop") {
        return "Trả góp";
    }
    if ($value == "No") {
        return "Nợ ngắn hạn";
    }
}
//kiem tra hoan don con ho hay ko
//return false neu con no
//return int neu chua tra xong
function kiem_tra_no_hoa_don($hoadonid = 0) {
    if ($hoadonid > 0) {
        $hoadon = hoadondb::get_by_id($hoadonid);
        $list_hoadon_thanhtoan = hoadonthanhtoandb::find_by_hoadon_id($hoadonid);
        $tong_thanh_toan = 0;
        for ($i = 0; $i < count($list_hoadon_thanhtoan); $i++) {
            $hoadon_thanhtoan = new hoadonthanhtoan();
            $hoadon_thanhtoan = $list_hoadon_thanhtoan[$i];
            $tong_thanh_toan += $hoadon_thanhtoan->sotien;
        }
        if ($tong_thanh_toan == $hoadon->tonghoadonso) {
            return -1;
        } else {
            return ($hoadon->tonghoadonso - $tong_thanh_toan);
        }
    }
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
        <link href="../web/css/style.css" rel="stylesheet" type="text/css" />
        <title>Quan ly hoa don</title>

    </head>
    <body>
        <div id="container">
            <? include '../web/header.php' ?>
            <div id="main">
                <h3>Quản lý hóa đơn</h3>
                <form action="" method="get">
                    <table class="fromTable">
                        <tr>
                            <td colspan="2" >
                                 <b>Hiển thị </b>
                                <select name="pay">
                                    <option value="1">Tất cả</option>
                                    <option value="2">Còn nợ</option>
                                </select>
                                <hr/>
                               
                            </td>
                        </tr>
                        <tr>
                            <td><input type="text" class="fromInputSize4" name="search_keys" value="<?= $search_keys; ?>" /> </td>
                            <td><input type="submit"  name="btn_search" value="Tìm kiếm"/> </td>
                        </tr>
                        <tr>
                            <td colspan="2" >
                                <hr/>
                            </td>
                        </tr>
                    </table>
                </form>

                <?
                if (!empty($list_hoadon)) {
                    $hoadon = new hoadon();
                ?>
                    <table class="listTable">
                        <thead>
                            <tr>
                                <th>STT</th>
                                <th class="fromInputSize2">Tên khách hàng</th>
                                <th>Ngày tháng</th>
                                <th>Tổng hóa đơn</th>
                                <?//<th>Loại thanh toán</th>?>
                                <th>Tình trạng</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                        <?
                        for ($i = 0; $i < count($list_hoadon); $i++) {
                            $hoadon = $list_hoadon[$i];
                            $tinh_trang = kiem_tra_no_hoa_don($hoadon->id);
                            $tinh_trang_hoa_don = "";
                            if ($tinh_trang > 0) {

                                $tinh_trang_hoa_don = "- ". formatMoney(kiem_tra_no_hoa_don($hoadon->id));
                            } else {
                                $tinh_trang_hoa_don = "Đã Trả đủ";
                            }
                        ?>
                            <tr>
                                <td align="center"><?= $i + 1; ?></td>
                                <td><a href="quanlyhoadon_xemchitiet.php?id=<?= $hoadon->id; ?>"> <?= $hoadon->tenkhachhang; ?> </a> </td>
                                <td><?= date("d-m-Y", strtotime($hoadon->ngaythang)); ?></td>
                                <td align="center" class="fromInputMoney"><?= formatMoney($hoadon->tonghoadonso); ?></td>
                                <?/*<td align="center"><?= hien_thi_thanh_toan($hoadon->phuongthucthanhtoan); ?></td> */?>
                                <td align="left"><?=$tinh_trang_hoa_don?></td>
                                <td align="center"> <a href="?page=<?=$page?>&<?=$querykey?>&del=true&id=<?= $hoadon->id; ?>" onclick="javascript:return confirm('Xac nhan xoa du lieu ?')"> Xóa </a></td>
                                
                            </tr>

                        <? } ?>
                      
                    </tbody>
                </table>
                <br/>
                <? show_pager_control($totals, $offset, $page,$show_page_num ,$page_start, $page_end,$querykey); ?>
                <? } else
                    echo  '<b>'.$search_keys.'</b> Không tìm thấy!'
                        ?>
                    </div>
            <? include '../web/footer.php'; ?>
        </div>
    </body>
</html>
